package guiLayer.extensions;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import modelLayer.Forest;
import modelLayer.Owner;

public class OwnerTableModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	private ArrayList<Owner> owners;
	
	public OwnerTableModel(ArrayList<Owner> own){
		this.owners = own;
	}

	public void refresh(ArrayList<Owner> o) {
		this.owners = o;
	}

	public int getColumnCount() {
		return 5;
	}

	public int getRowCount() {
		return owners.size();
	}

	public Object getValueAt(int rowIndex, int collIndex) {
		Owner o  = owners.get(rowIndex);
		Object value = null;
		if(collIndex == 0){
			value = o.getId();
		} else if(collIndex == 1){
			value = o.getName();
		} else if(collIndex == 2){
			value = o.getAddress();
		} else if(collIndex == 3){
			value = o.getPhoneNumber();
		} else if(collIndex == 4){
			ArrayList<Forest> forests = o.getForests();
			if(forests.size() > 0){
				String fors = "";
				for(Forest f : forests){
					fors += f.getName() + ", ";
				}
				fors = fors.substring(0, fors.lastIndexOf(","));
				value = fors;
			}else{
				value = "";
			}
		}
		return value;
	}	
	
	public String getColumnName(int collIndex){
		String value = "??";
		if(collIndex == 0){
			value = "ID";
		} else if(collIndex == 1){
			value = "Name";
		} else if(collIndex == 2){
			value = "Address";
		} else if(collIndex == 3){
			value = "Phone Number";
		} else if(collIndex == 4){
			value = "Forests";
		}
		return value;
	}
}
